package com.fingard.dsp.bank.directbank.jscb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.apache.commons.lang.StringUtils;

public class JSCB01SinglePay extends JSCB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

        try {
            JSCB01Header header = new JSCB01Header();
            header.transCode = "300001";
            header.reqSeqNo = p_req1908.singleDetail.askNumber;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeader(xmlWriter, header);
            XElement tmpXElement = new XElement("body", "");
            tmpXElement.addChild(new XElement("pay_acno", p_req1908.singleDetail.payAct));             //付款账号
            tmpXElement.addChild(new XElement("proxy_pay_acno", ""));                        //代理付款帐号
            tmpXElement.addChild(new XElement("pay_cur_code", convertCurToBank(p_req1908.singleDetail.payCur)));//付款币种
            tmpXElement.addChild(new XElement("pay_acname", p_req1908.singleDetail.payName));///付款户名
            tmpXElement.addChild(new XElement("proxy_pay_acname", ""));                        //代理付款户名
            tmpXElement.addChild(new XElement("proxy_pay_orgname", ""));                       //代理付款开户行
            tmpXElement.addChild(new XElement("pay_accaddr", ""));                              //付款地址
            tmpXElement.addChild(new XElement("as_flag", ""));             //账簿标志
            tmpXElement.addChild(new XElement("as_acno", ""));             //账簿号
            tmpXElement.addChild(new XElement("as_acname", ""));             //账簿名称
            tmpXElement.addChild(new XElement("cert_type", p_req1908.singleDetail.certType));             //凭证种类
            tmpXElement.addChild(new XElement("cert_no", p_req1908.singleDetail.askNumber));                 //凭证种类
            tmpXElement.addChild(new XElement("rcv_acno", p_req1908.singleDetail.recAct));                 //收款账号
            tmpXElement.addChild(new XElement("rcv_cur_code", convertCurToBank(p_req1908.singleDetail.recCur))); //收款币种
            tmpXElement.addChild(new XElement("rcv_acname", p_req1908.singleDetail.recName));             //收款户名
            tmpXElement.addChild(new XElement("rcv_accaddr", ""));                             //收款人地址
            tmpXElement.addChild(new XElement("rcv_bank_no", p_req1908.singleDetail.cnapsCode));          //收款行号
            tmpXElement.addChild(new XElement("rcv_bank_name", p_req1908.singleDetail.recBankName));     //收款行名
            tmpXElement.addChild(new XElement("amt", p_req1908.singleDetail.payAmount));                   //交易金额
            tmpXElement.addChild(new XElement("bank_flag", "1".equals(p_req1908.singleDetail.sameBnk) ? "0" : "1"));    //银行标志
            tmpXElement.addChild(new XElement("urgency_flag", p_req1908.singleDetail.paySpd));                           //加急标志
            tmpXElement.addChild(new XElement("area_flag", "1".equals(p_req1908.singleDetail.sameCity) ? "0" : "1"));   //同城异地标志
            tmpXElement.addChild(new XElement("bank_kind", ""));                                //银行类别
            String tmpPourse = StringUtils.isEmpty(p_req1908.singleDetail.purpose) ? "无" : p_req1908.singleDetail.purpose;
            tmpXElement.addChild(new XElement("purpose", tmpPourse));                                        //用途
            tmpXElement.addChild(new XElement("postscript", p_req1908.singleDetail.abstractATS));         //附言
            nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String succ = tmpBkReader.getFirstText("/ap/head/succ_flag");
                    if("1".equals(succ) || "8".equals(succ)){
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    }else {
                        String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                        retDetail.respCode = "0";
                        retDetail.transState = tmpRetCode+"+" +tmpBkReader.getFirstText("/ap/body/stat");
                        if("A".equals(tmpBkReader.getFirstText("/ap/body/stat"))){
                            retDetail.payInfoCode = "I4001";
                            retDetail.respInfo = tmpBkReader.getFirstText("/ap/body/luodi_info");
                        }else if("9".equals(tmpBkReader.getFirstText("/ap/body/stat"))){
                            retDetail.respInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                            retDetail.payInfoCode = "I4001";
                        }else {
                            retDetail.payInfoCode = tmpBkReader.getFirstText("/ap/head/ret_info");
                            retDetail.respInfo = tmpBkReader.getFirstText("/ap/body/ext_info");
                        }
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (retStrArr[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = retStrArr[1];
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = FailCode.E9999;
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = retStrArr[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retDetail.respCode = "-1";
            retDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1944);

        try {
            JSCB01Header bankHeader = new JSCB01Header();
            bankHeader.transCode = "200205";
            bankHeader.reqSeqNo = p_req1944.reqHeader.transSeqID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeader(xmlWriter, bankHeader);

            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("cert_no", p_req1944.askNumber);
            xmlWriter.writeElementClosed("req_no");
            xmlWriter.writeElementString("tr_acdt", Format.Date8Format.format(p_req1944.getBgnDateTime()));
            xmlWriter.writeElementString("cms_corp_no", "");
            xmlWriter.writeElementString("acno", p_req1944.payAct);
            xmlWriter.writeElementString("cur_code", convertCurToBank(p_req1944.payCur), true);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    if ("0000".equals(tmpRetCode)) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpBkReader.getFirstText("/ap/body/error_info");
                        tmpRetDetail.transState = tmpRetCode + "+" + tmpBkReader.getFirstText("/ap/body/stat");
                        tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/ap/body/error_info");
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = retStrArr[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
